package com.tech.connect.zhaofuwu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ksy.common.dialog.BaseDialog;
import com.ksy.common.dialog.DateChooseDialog;
import com.ksy.common.image.ImageLoader;
import com.tech.connect.R;
import com.tech.connect.activity.BaseActivity;
import com.tech.connect.activity.EditIntNumActivity;
import com.tech.connect.activity.EditTextActivity;
import com.tech.connect.api.BaseEntityOb;
import com.tech.connect.api.FuWuHttp;
import com.tech.connect.api.UserInfo;
import com.tech.connect.model.ItemFuWu;

import java.io.Serializable;
import java.util.Map;

public class FuWuYueDanActivity extends BaseActivity {


    private ImageView ivHead;
    private TextView tvName;
    private ImageView ivSex;
    private TextView tvAge;
    private TextView tvHeight;
    private TextView tvWeight;
    private TextView tvStudy;
    private TextView tvMarry;
    private TextView tvCatName;
    private TextView tvMoney;
    private TextView tvNum;
    private TextView tvDate;
    private TextView tvAddress;
    private TextView tvTotalPrice;
    private View btEnter;
    private ItemFuWu item;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getHeadBar().setTitle("服务约单");
        setContentView(R.layout.activity_fuwu_yuedan);
        Intent intent = getIntent();
        Serializable data = intent.getSerializableExtra("data");
        if (data == null || !(data instanceof ItemFuWu)) {
            showToast("数据有误");
            destroyActivity();
            return;
        }
        item = (ItemFuWu) data;
        initView();
        initData();
    }

    private void initData() {
        UserInfo user = item.user;
        if (user != null) {
            ImageLoader.getInstance().loadHead(activity, user.headImage, ivHead);
            tvName.setText(user.nickName);
            if (user.gender == 0) {
                ivSex.setImageResource(R.drawable.icon_women);
            } else {
                ivSex.setImageResource(R.drawable.icon_man);
            }
            tvAge.setText(String.valueOf(user.age));
            tvHeight.setText(user.height + "cm");
            tvWeight.setText(user.weight + "kg");
            tvStudy.setText(user.educationName);
            tvMarry.setText(user.marriageName + "");
        } else {
            tvName.setText("");
            tvAge.setText("");
            tvHeight.setText("");
            tvWeight.setText("");
            tvStudy.setText("");
            tvMarry.setText("");
        }
        ItemFuWu.CatServer serviceCategoryName = item.serviceCategoryName;
        if (serviceCategoryName != null) {
            tvCatName.setText(serviceCategoryName.name);
        } else {
            tvCatName.setText("");
        }
        Spanned money = Html.fromHtml("<font color='#f49930'>" + item.price + "</font>" + "<font color='#666666'>" + item.typeName + "</font>");
        tvMoney.setText(money);
    }

    private void initView() {
        ivHead = findViewById(R.id.ivHead);
        tvName = findViewById(R.id.tvName);
        ivSex = findViewById(R.id.ivSex);
        tvAge = findViewById(R.id.tvAge);
        tvHeight = findViewById(R.id.tvHeight);
        tvWeight = findViewById(R.id.tvWeight);
        tvStudy = findViewById(R.id.tvStudy);
        tvMarry = findViewById(R.id.tvMarry);
        tvCatName = findViewById(R.id.tvCatName);
        tvMoney = findViewById(R.id.tvMoney);
        tvNum = findViewById(R.id.tvNum);
        tvDate = findViewById(R.id.tvDate);
        tvAddress = findViewById(R.id.tvAddress);
        tvTotalPrice = findViewById(R.id.tvTotalPrice);
        btEnter = findViewById(R.id.btEnter);
        btEnter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createOrder();
            }
        });
        tvAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, EditTextActivity.class);
                intent.putExtra("data", tvAddress.getText().toString().trim());
                jump2Activity(intent, 3);
            }
        });
        tvDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DateChooseDialog dateChooseDialog = new DateChooseDialog(activity);
                dateChooseDialog.setOnClickListener(new DateChooseDialog.ClickListener() {
                    @Override
                    public void enter(BaseDialog dialog, DateChooseDialog.Year y, DateChooseDialog.Month m, DateChooseDialog.Day d) {
                        dialog.dismiss();
                        tvDate.setText(y + "-" + m + "-" + d);
                    }
                });
                dateChooseDialog.show();
            }
        });
        tvNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, EditIntNumActivity.class);
                intent.putExtra("data", tvNum.getText().toString().trim());
                jump2Activity(intent, 4);
            }
        });
    }

    private void createOrder() {
        String address = tvAddress.getText().toString().trim();
        String date = tvDate.getText().toString().trim();
        String numTrim = tvNum.getText().toString().trim();
        if (isEmpty(numTrim)) {
            showToast("请输入数量");
            return;
        }
        int num = Integer.parseInt(numTrim);
        if (num <= 0) {
            showToast("请输入数量");
            return;
        }
        if (isEmpty(date)) {
            showToast("请选择服务时间");
            return;
        }
        if (isEmpty(date)) {
            showToast("请输入服务地点");
            return;
        }
        Map<String, Object> map = FuWuHttp.getMap();
        map.put("address", address);
        map.put("number", num);
        map.put("serviceId", item.id);
        map.put("serviceTime", date);
        FuWuHttp.createOrder(map, new BaseEntityOb<String>() {
            @Override
            public void onStart() {
                super.onStart();
                showDialog();
            }

            @Override
            public void onDataDeal(boolean isSuccess, String data, String msg) {
                hideDialog();
                showToast(msg);
                if (isSuccess) {
                    setResult(RESULT_OK);
                    destroyActivity();
                }

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 3) {
                String data = intent.getStringExtra("data");
                tvAddress.setText(data);
            } else if (requestCode == 4) {
                String data = intent.getStringExtra("data");
                int num = Integer.parseInt(data);
                tvTotalPrice.setText("￥" + String.valueOf(num * item.price));
                tvNum.setText(data);

            }
        }
    }
}